package xyz.liut.liutlibraries.baseclass;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import xyz.liut.volley.LiutVolley;

/**
 * Created by liut on 2016/5/31.
 */
public class BaseFragment extends Fragment {

    protected String TAG;
    protected Gson gson;
    protected LiutVolley volley;
    protected Activity mContext;


    private PopupWindow popupWindow;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gson = new Gson();
        TAG = testGetFunctionName();
        volley = LiutVolley.getInstance(getContext());
    }


    protected void showProgress(View view) {
        if (popupWindow == null) {
            popupWindow = new PopupWindow(mContext);
            popupWindow.setContentView(new ProgressBar(mContext));
            popupWindow.setFocusable(false);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        }
        if (!popupWindow.isShowing()) {
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    protected void hideProgress() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }


    protected void showToast(String content) {
        Toast.makeText(getContext(), content, Toast.LENGTH_SHORT).show();
    }

    protected void showToast(int content) {
        Toast.makeText(getContext(), content + "", Toast.LENGTH_SHORT).show();
    }

    protected void showSnackBar(View view, String content) {
        Snackbar.make(view, content, Snackbar.LENGTH_SHORT).show();
    }

    protected void showSnackBar(View view, int content) {
        Snackbar.make(view, content + "", Snackbar.LENGTH_SHORT).show();
    }

    public String testGetFunctionName() {
        // 方法1：通过Throwable的方法getStackTrace()
        return new Throwable().getStackTrace()[1].getClassName();
    }


}
